package com.wanxi.service.impl;

import com.wanxi.mapper.UserMapper;
import com.wanxi.model.Common;
import com.wanxi.model.Message;
import com.wanxi.model.User;
import com.wanxi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public Message<User> getUserList(Map<String,Object> map) {
        int page = Integer.parseInt((String) map.get("page"));
        int size = Integer.parseInt((String) map.get("size"));
        map.put("page",(page-1)* size);
        map.put("size",size);
        Message message = new Message();
        message.setList(userMapper.getUserList(map));
        message.setTotal(userMapper.getUserCount(map));
        message.setModel(map);
        if (message.getList()!=null)
            message.setCode(200);
        else
            message.setCode(500);
        return message;
    }

    @Override
    public User getUserById(Integer id) {
        return userMapper.getUserById(id);
    }

    @Override
    public int addUser(User user) {
        int result = userMapper.addUser(user);
        if (result > 0)
            return 200;
        else
            return 500;
    }

    @Override
    public Message login(User user) {
        Message message = new Message();
        Map<String, Object> map = new HashMap<>();
        user = userMapper.login(user);
        if (user == null)
            message.setCode(500);
        else
            message.setCode(200);
        map.put("user", user);
        message.setModel(map);
        return message;
    }

    @Override
    public Message getUserByUsername(String username) {
        Message message = new Message();
        Map<String, Object> map = new HashMap();
        User user = userMapper.getUserByUsername(username);
        if (user != null) {//如果不为空，则用户名已被占用
            message.setCode(500);
        } else if (user == null)
            message.setCode(200);
        map.put("user", user);
        message.setModel(map);
        return message;
    }

    @Override
    public int editUser(Map map) {
        int result = userMapper.editUser(map);
        if (result > 0)
            return 200;
        else
            return 500;
    }

    @Override
    public List<User> getUserByTeam(String team) {
        return userMapper.getUserByTeam(team);
    }

    @Override
    public int updateUserTeam(Map map) {
        String member = ((String) map.get("members")).replace("\"", "");
        String[] split = member.substring(1, member.length() - 1).split(",");
        System.out.println(split);
        List<String> list = new ArrayList<>();
        for (String str : split) {
            list.add(str);
        }
        System.out.println(list);
        map.put("member", split);
        if (userMapper.updateUserTeam(map) > 0)
            return 200;
        else
            return 500;
    }

    @Override
    public int updateStatus(Map map) {
        int result = userMapper.updateStatus(map);
        if (result>0)
            return 200;
        else
            return 500;
    }

    @Override
    public int deleteUsers(Map map) {
        String arrays = ((String) map.get("arrays")).replace("[","").replace("]","");
        String[] split = arrays.split(",");
        List list = Arrays.asList(split);
        int result = userMapper.deleteUsers(list);
        if (result>0)
            return 200;
        else
            return 500;
    }
}
